package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.TaskFacilitiesRelationDao;
import cn.topevery.garden.entity.po.TaskFacilitiesRelation;
import cn.topevery.garden.entity.query.TaskFacilitiesRelationQuery;
import cn.topevery.garden.entity.vo.TaskFacilitiesRelationVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.DataFillHelper;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbParameter;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务与设施关系
 *
 * @author code-generate
 * @date 2021-08-31
 */
@Repository
public class TaskFacilitiesRelationDaoImpl extends BaseDaoImpl<TaskFacilitiesRelation> implements TaskFacilitiesRelationDao {
    @Override
    public <P extends DbCsp> List<TaskFacilitiesRelation> getSearch(P para) {
        List<TaskFacilitiesRelation> res = new ArrayList<>();
        String table = "t_task_facilities_relation ttfr";
        String columns = "ttfr.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttfr.c_id desc");
        sqlQueryMate.where(true, "ttfr.c_db_status", ScriptConditionEnum.Eq, 0);

        if(para.whereMap!=null){
            para.whereMap.forEach(f->{
                if(f.key=="taskId"){
                    sqlQueryMate.where(true, "ttfr.c_task_id", ScriptConditionEnum.Eq, f.value);
                }
            });
        }

        getDbExecute().run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            for (DbParameter i : sqlQueryMate.getParameters()) {
                dbCommand.addParameter(i);
            }
            DataFillHelper.readToList(dbCommand.executeReader(), res, TaskFacilitiesRelation.class);
        });
        return res;
    }

    

    @Override
    public DataPager<TaskFacilitiesRelationVO> listPage(TaskFacilitiesRelationQuery query) {
        String table = "t_task_facilities_relation ttfr";

        String columns = "ttfr.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttfr.c_id desc");
        sqlQueryMate
                .where(true, "ttfr.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, query.pageSize, query.pageIndex, TaskFacilitiesRelationVO.class);
    }
}
